﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

/// <summary>
/// 单例模式基类 目的是避免代码的冗余 方便实现我们单例模式的类
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseManager<T> where T : class//, new()
{
    public static T instance;

    //用于加锁的对象
    protected static readonly object lockObj = new object();

    //通过属性的方式实现
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                lock (lockObj)
                {
                    if (instance == null)
                    {
                        //instance = new T();
                        //利用反射得到无参私有的构造函数 用来于对象的实例化
                        Type type = typeof(T);
                        ConstructorInfo info = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
                                                                    null,
                                                                    Type.EmptyTypes,
                                                                    null);

                        if (info != null)
                            instance = info.Invoke(null) as T;
                        else
                            Debug.Log("没有得到对应的无参构造函数");
                    }
                }
            }
            return instance;
        }
    }

    //通过方法的方式实现
    //public static T GetInstance()
    //{
    //    if(instance == null)
    //        instance= new T();
    //    return instance;
    //}
}
